package com.appscomm.bluetoothdemo;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;


import com.appscomm.bluetoothdemo.application.BaseApplication;
import com.appscomm.bluetoothdemo.util.UIUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    @ViewInject(R.id.lv_device_list)
    private ListView deviceList;
    @ViewInject(R.id.bt_scan_device)
    private Button scanDevice;

    private BluetoothAdapter bluetoothAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        init();
        scanDevice.setOnClickListener(this);
    }

    private void init() {
        this.bluetoothAdapter = BaseApplication.bluetoothAdapter;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_scan_device:
                startScanDevice();
                break;
            case R.id.bt_stop_scan_device:
                stopScanDevice();
                break;
            default:
                break;
        }
    }

    private void startScanDevice() {
        bluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback(){
            @Override
            public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
                Toast.makeText(MainActivity.this,"扫描",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void stopScanDevice() {
        Log.i("进入停止扫描","");
        bluetoothAdapter.stopLeScan(new BluetoothAdapter.LeScanCallback(){
            @Override
            public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
                Toast.makeText(MainActivity.this,"停止扫描",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
